package com.xhh.nphm.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.IMmailWorkFormTempService;
import com.xhh.nphm.system.service.IMwfJudgeService;
import com.xhh.nphm.system.service.MMailWorkFormLogService;
import com.xhh.nphm.system.service.MmailWorkFormService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/tVstRec")
public class TVstRecMailController extends BaseController {
    private String prefix = "system/tVstRec";

    @Autowired
    private MmailWorkFormService mmailWorkFormService;
    @Autowired
    private IMmailWorkFormTempService mmailWorkFormTempService;
    @Autowired
    private IMwfJudgeService mwfJudgeService;
    @Autowired
    private MMailWorkFormLogService mMailWorkFormLogService;

    /**
     * 待回访信件工单 页面
     */
    @GetMapping("/mail/backWaitList")
    public ModelAndView backWaitList(ModelMap map) {
        map.put("num", mmailWorkFormService.countBackWaitList());
        return new ModelAndView(prefix + "/mail/backWaitList");
    }

    /**
     * 待回访信件工单 列表数据
     *
     * @param mmailWorkForm
     * @return
     */
    @PostMapping("mail/backWaitListJson")
    public TableDataInfo backWaitListJson(MmailWorkForm mmailWorkForm) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<MmailWorkForm> list = mmailWorkFormService.backWaitListJson(user.getUserNo(), mmailWorkForm);
        return getDataTable(list);
    }

    /**
     * 随机获取待回访信件工单 页面
     *
     * @param map
     * @return
     */
    @GetMapping("/mail/countBackWaitList")
    public ModelAndView countBackWaitList(ModelMap map) {
        map.put("num", mmailWorkFormService.countBackWaitList());
        return new ModelAndView(prefix + "/mail/countBackWaitList");
    }

    /**
     * 随机获取N条待回访信件工单 并保存到中间表中
     */
    @PostMapping("mail/getBackWaitList")
    @RequestBody
    public Object getBackWaitList(Integer num) {
        SysUser user = ShiroUtils.getSysUser();
        List<MmailWorkForm> list = new ArrayList<>();
        AjaxResult ajaxResult = new AjaxResult();
        try {
            list = mmailWorkFormService.getBackWaitList(num);
            for (MmailWorkForm mmailWorkForm : list) {
                MmailWorkFormTemp temp = new MmailWorkFormTemp();
                temp.setWfId(Integer.valueOf(mmailWorkForm.getPkId()));
                temp.setUserNo(user.getUserNo());
                temp.setUserName(user.getUserName());
                temp.setCreateTime(new Date());
                mmailWorkFormTempService.save(temp);
            }
            ajaxResult.put("msg", "已成功获取到" + list.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult.put("code", 0);
        return ajaxResult;
    }

    /**
     * 待回访信件工单 回访页面
     */
    @GetMapping("/mail/backVisitEdit/{pkId}")
    public ModelAndView backVisitEdit(@PathVariable Integer pkId, ModelMap map) {
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(pkId);
        map.put("mmailWorkForm", mmailWorkForm);
        MwfJudgeEty mwfJudgeEty = mwfJudgeService.getByWfId(pkId);
        if (mwfJudgeEty != null) {
            map.put("mwfJudgeEty", mwfJudgeEty);
        }
        return new ModelAndView(prefix + "/mail/backVisitEdit");
    }

    /**
     * 待回访信件工单 提交回访数据
     * @param mwfJudgeEty
     * @return
     */
    @PostMapping("mail/backVisitEditJson")
    @RequestBody
    public AjaxResult backVisitEditJson(MwfJudgeEty mwfJudgeEty) {
        String desc = "";
        SysUser user = ShiroUtils.getSysUser();
        UpdateWrapper<MmailWorkForm> updateWrapper = new UpdateWrapper<>();
        if (mwfJudgeEty.getVstLostC().equals("10")) {
            if (mwfJudgeEty.getJudgeC().equals("1")) {
                desc = "评价：不满意";
                updateWrapper.set("CrtStepID", 905)
                        .set("CrtStepName", "回访不满意")
                        .set("CrtStepTime", new Date())
                        .set("VisitFlag", 21)
                        .set("RecordFlag", 20)
                        .set("RecordTime", new Date())
                        .set("RecUserNo", user.getUserNo())
                        .set("RecUserName", user.getUserName())
                        .eq("PkId", mwfJudgeEty.getWfId());
            } else {
                desc = "评价：满意";
                updateWrapper.set("CrtStepID", 950)
                        .set("CrtStepName", "归档(评价满意)")
                        .set("CrtStepTime", new Date())
                        .set("VisitFlag", 22)
                        .set("RecordFlag", 22)
                        .set("RecordTime", new Date())
                        .set("RecUserNo", user.getUserNo())
                        .set("RecUserName", user.getUserName())
                        .eq("PkId", mwfJudgeEty.getWfId());
            }
        } else {
            desc = mwfJudgeEty.getVstLostN();
            updateWrapper.set("CrtStepID", 901)
                    .set("CrtStepName", "回访失败")
                    .set("CrtStepTime", new Date())
                    .set("VisitFlag", 22)
                    .eq("PkId", mwfJudgeEty.getWfId());
        }
        updateWrapper.set("IsRecord",1);
        boolean flag = mmailWorkFormService.update(null, updateWrapper);
        mwfJudgeService.save(mwfJudgeEty);
        if (flag) {
            MMailWorkFormLog etyLog = new MMailWorkFormLog();
            etyLog.setWfId(mwfJudgeEty.getWfId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnID(-1);
            etyLog.setOpAction("录入回访");
            etyLog.setWsDesc("录入回访结果");
            etyLog.setOpDesc("录入回访结果（" + desc + "）");
            etyLog.setWsOpen(0);
            etyLog.setEffectFlag(1);
            etyLog.setLogTime(new Date());
            mMailWorkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }
    /**
     * 已回访信件工单 列表页面
     */
    @GetMapping("/mail/backOverList")
    public ModelAndView backOverList() {
        return new ModelAndView(prefix+"/mail/backOverList");
    }
    /**
     * 已回访信件工单列表数据
     */
    @PostMapping("mail/backOverListJson")
    public TableDataInfo backOverListJson(MmailWorkForm mmailWorkForm) {
        startPage();
        List<MmailWorkForm> list=mmailWorkFormService.backOverListJson(mmailWorkForm);
        return getDataTable(list);
    }
}
